#include "chooselevelscene.h"
#include <QMenuBar>
#include <QPainter>
#include "mypushbutton.h"
#include <QTimer>
#include <QLabel>


ChooseLevelScene::ChooseLevelScene(QWidget *parent) : QMainWindow(parent)
{
    //set选择关卡场景
    this->setFixedSize(320,588);
    //图标
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));

    //标题
    this->setWindowTitle("选择关卡场景");

    //创建菜单栏
    QMenuBar *bar = menuBar();
    setMenuBar(bar);

    QMenu *startMenu = bar->addMenu("开始");
    QAction *quitAction = startMenu->addAction("退出");

    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });

    //返回按钮格式位置
    MyPushButton * closeBtn = new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    closeBtn->setParent(this);
    closeBtn->move(this->width()-closeBtn->width(),this->height()-closeBtn->height());

    //返回按钮功能实现
        connect(closeBtn,&MyPushButton::clicked,[=](){
            //延时返回
            QTimer::singleShot(500, this,[=](){
                this->hide();
                //触发自定义信号，关闭自身，该信号写到 signals下做声明
                emit this->chooseSceneBack();
            });
        });

    //创建选择关卡的按钮

    for(int i = 0 ; i < 20;i++)
   {
       MyPushButton * menuBtn = new MyPushButton(":/res/LevelIcon.png");
       menuBtn->setParent(this);
       menuBtn->move(25 + (i%4)*70 , 130+ (i/4)*70);

       //监听选择关卡按钮的信号槽
       connect(menuBtn,&MyPushButton::clicked,[=](){
          // qDebug() << "select: " << i;
           if(play == nullptr)  //游戏场景最好不用复用，直接移除掉创建新的场景
           {
               this->hide();//将选择关卡的场景隐藏
               play = new PlayScene(i+1); //将选择的关卡号 传入给PlayerScene
               play->show();
           }
           //监听游戏场景中的back按钮
           connect(play,&PlayScene::chooseSceneBack,[=](){
                               this->show();
                               delete play;//将原本的游戏场景释放
                               play = nullptr;
                           });

       });

       //按钮上显示的文字
       QLabel * label = new QLabel;
       label->setParent(this);
       label->setFixedSize(menuBtn->width(),menuBtn->height());//文字大小
       label->setText(QString::number(i+1));//文字内容
       label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter); //设置居中
       label->move(25 + (i%4)*70 , 130+ (i/4)*70);
       label->setAttribute(Qt::WA_TransparentForMouseEvents,true);  //鼠标事件穿透，按钮被文字遮挡了直接connect不行
   }


}
void ChooseLevelScene::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/OtherSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

     //加载标题
    pix.load(":/res/Title.png");
    painter.drawPixmap( (this->width() - pix.width())*0.5,30,pix.width(),pix.height(),pix);
}
